package top.continew.admin.webapi.jobfair;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.common.model.dto.LoginUser;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.admin.jobfair.model.entity.ResumeDO;
import top.continew.admin.system.model.resp.FileUploadResp;
import top.continew.admin.system.service.FileService;
import top.continew.starter.core.util.validate.ValidationUtils;
import top.continew.starter.extension.crud.enums.Api;

import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.web.bind.annotation.*;

import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.admin.jobfair.model.query.ResumeQuery;
import top.continew.admin.jobfair.model.req.ResumeReq;
import top.continew.admin.jobfair.model.resp.ResumeDetailResp;
import top.continew.admin.jobfair.model.resp.ResumeResp;
import top.continew.admin.jobfair.service.ResumeService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;

/**
 * 简历管理 API
 *
 * @author xiahaike
 * @since 2024/09/11 10:27
 */
@Tag(name = "简历管理 API")
@RestController
@CrudRequestMapping(value = "/jobfair/resume", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE, Api.EXPORT})
public class ResumeController extends BaseController<ResumeService, ResumeResp, ResumeDetailResp, ResumeQuery, ResumeReq> {

    @Resource
    private FileService fileService;

    @Resource
    private ResumeService resumeService;

    @Operation(
            summary = "分页查询列表",
            description = "分页查询列表"
    )
    @ResponseBody
    @GetMapping
    public R<PageResp<ResumeResp>> page(ResumeQuery query, @Validated PageQuery pageQuery) {
        this.checkPermission(Api.LIST);
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long id = loginUser.getId();
        query.setStudentId(id);
        return R.ok(this.baseService.page(query, pageQuery));
    }


    @ResponseBody
    @GetMapping("/list/{studentId}")
    public R<List<ResumeDO>> listByStudentId(@PathVariable Long studentId) {
        this.checkPermission(Api.LIST);
        // todo 优化
        List<ResumeDO> list = resumeService.listByStudentId(studentId);
        if (list.isEmpty()) {
            return R.fail("暂无简历数据，请先上传简历");
        }
        return R.ok(list);
    }

    @Operation(summary = "上传简历", description = "上传简历")
    @PostMapping("/uploadFile")
    public R<FileUploadResp> upload(@NotNull(message = "文件不能为空") MultipartFile file) {
        // 上传文件
        ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
        FileInfo fileInfo = fileService.upload(file);
        // 保存简历信息
        LoginUser loginUser = LoginHelper.getLoginUser();
        Long id = loginUser.getId();
        ResumeReq resumeReq = new ResumeReq();
        resumeReq.setFileId(Long.valueOf(fileInfo.getId()));
        resumeReq.setStudentId(id);
        resumeReq.setName(fileInfo.getOriginalFilename());
        resumeService.add(resumeReq);
        return R.ok(FileUploadResp.builder().url(fileInfo.getUrl()).build());
    }

    @Operation(
            summary = "删除数据",
            description = "删除数据"
    )
    @Parameter(
            name = "ids",
            description = "ID 列表",
            example = "1,2",
            in = ParameterIn.PATH
    )
    @Override
    @ResponseBody
    @DeleteMapping({"/{ids}"})
    public R<Void> delete(@PathVariable List<Long> ids) {
        this.checkPermission(Api.DELETE);
        // 根据ids批量查询文件id
        List<ResumeDO> resumeDOList = resumeService.listByIds(ids);
        List<Long> fileIds = resumeDOList.stream().map(ResumeDO::getFileId).toList();
        // 删除简历信息
        fileService.delete(fileIds);
        this.baseService.delete(ids);
        return R.ok("删除成功");
    }
}